<template>
  <div class="wrapper wrapper-content animated fadeInRight v-cloak" v-cloak>
    <div class="col-md-12">
      <form @submit.prevent="save()" accept-charset="UTF-8" class="form m-t" id="authPermForm" method="post">
        <input name="id" type="hidden" v-model="item.id"/>
        <input name="token" type="hidden" v-model="item.uuidToken"/>
        <input :value="newFlag!=0?0:1" name="newFlag" type="hidden"/>
        <div class="clearfix "></div>
        <div class="col-sm-3">
          <div class="form-group clearfix">
            <label class="col-md-8 control-label">权限名称：</label>
            <div class="col-md-12">
              <input aria-describedby="bean-name-error" aria-invalid="true" aria-required="true" class="form-control" id="bean-name" maxlength="50" name="name" placeholder="请输入权限名称" required="" type="text" v-model="item.name">
              <label class="help-block m-b-none" for="bean-name" id="bean-name-error"></label>
            </div>
          </div>

          <div class="form-group clearfix">
            <label class="col-md-8 control-label">权限匹配符：</label>
            <div class="col-md-12">
              <input aria-describedby="bean-matchStr-error" aria-invalid="true" aria-required="true" class="form-control" id="bean-matchStr" maxlength="50" name="matchStr" placeholder="请输入权限匹配符" required="" type="text"
                     v-model="item.matchStr">
              <label class="help-block m-b-none" for="bean-matchStr" id="bean-matchStr-error"></label>
            </div>
          </div>

        </div>
        <div class="col-sm-3">

          <div class="form-group clearfix">
            <label class="col-md-8 control-label">父级id：</label>
            <div class="col-md-12">
              <input aria-describedby="bean-parentId-error" aria-invalid="true" class="form-control" id="bean-parentId" maxlength="32" name="parentId" placeholder="请输入父级id" type="text" v-model="item.parentId">
              <label class="help-block m-b-none" for="bean-parentId" id="bean-parentId-error"></label>
            </div>
          </div>

          <div class="form-group clearfix">
            <label class="col-md-8 control-label">备注：</label>
            <div class="col-md-12">
              <input aria-describedby="bean-memo-error" aria-invalid="true" class="form-control" id="bean-memo" maxlength="255" name="memo" placeholder="请输入备注" type="text" v-model="item.memo">
              <label class="help-block m-b-none" for="bean-memo" id="bean-memo-error"></label>
            </div>
          </div>

        </div>
        <div class="col-sm-3">
          <div class="form-group clearfix">
            <label class="col-md-8 control-label">排序：</label>
            <div class="col-md-12">
              <input aria-describedby="bean-orderNo-error" aria-invalid="true" class="form-control" id="bean-orderNo" name="orderNo" placeholder="请输入排序" type="text" v-model="item.orderNo">
              <label class="help-block m-b-none" for="bean-orderNo" id="bean-orderNo-error"></label>
            </div>
          </div>
        </div>
        <div class="clearfix "></div>
        <div class="col-12 text-center" style="height: 50px;">
          <div style="width: 200px; margin: 0 auto;text-align: center;">
            <input class="btn btn-primary" type="submit" value="保存"/> | <a @click="lbox.closeMyBoxLayer()" class="btn btn-danger" href="javascript:">返回</a>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>
<script type="text/javascript">
  import '@/assets/css/style.css'

  import $ from '@/assets/js/jquery-vendor.js'
  import axios from 'axios'
  import '@/assets/js/validate/validation-vendor.js'
  import {$storage, apiUtil, axiosContentType, site} from '@/assets/js/boss'

  export default {
    data() {
      return {
        item: {}, newFlag: 0, uuidToken: ''
      }
    },
    mounted() {
      let that = window.$vueApp = this;
      document.getElementsByTagName('body')[0].className = 'fixed-sidebar full-height-layout';
      that.staff.init(() => {
        $('#authPermForm').validate();
        that.staff.init(function () {
          that.newFlag = $storage.getItem(site.authPerm.info);
          that.uuidToken = apiUtil.guid();
          if (apiUtil.existSessionKey(site.authPerm.info)) {
            that.info();
          }
        });
      })
    },
    methods: {
      info() {
        let that = this;
        axios.post(site.authPerm.info + $storage.getItem(site.authPerm.info), {}).then(function (response) {
          const result = response.data;
          if (result.code === 0) {
            that.item = result.data;
          } else {
            alert(result.message);
          }
        });
      },
      save() {
        let that = this;
        if ($('#authPermForm').valid()) {
          axios.post(site.authPerm.save, $('#authPermForm').serialize()
            , axiosContentType.xWwwFormUrlencoded).then(function (response) {
            const result = response.data;
            // console.info("获取数据.." + JSON.stringify(result));
            if (result.code === 0) {
              parent.$vueApp.page();
              alert('保存成功!');
              that.lbox.closeMyBoxLayer()
            } else {
              alert(result.message);
            }
          });
        }
      }
    }
  }
</script>

